<?php if (!defined("_ECRIRE_INC_VERSION")) return;
if (function_exists('replace_template_fields')) return;

/**
*  Replaces template @fields@ with values
*  Besides $values array such substitutions are also evailable: @nom_site_spip@ - Name of site
*      
*  @param string $template Text of template
*  @param array $values Array of field=>value pairs. All @field@ snippets will be substituted with value from this array 
*/
function pdk_replace_template_fields($template, $values) {
    $nom_site_spip = supprimer_tags(typo($GLOBALS['meta']['nom_site']));
        
    $a_remplacer = array();
    if (preg_match_all('/@[\w]+@/', $template, $a_remplacer)){
        $a_remplacer = $a_remplacer[0];
	    foreach ($a_remplacer as $cle=>$val) $a_remplacer[$cle] = trim($val, '@');
            $a_remplacer = array_flip($a_remplacer);
		    $a_remplacer = array_intersect_key($values, $a_remplacer);
            // Additional replacements:
            $a_remplacer = array_merge($a_remplacer, array('nom_site_spip' => $nom_site_spip));
        }
		return typo(trim(_L($template, $a_remplacer)));
}